package com.lolo.presentation.service.metadata;

import java.io.IOException;
import java.net.HttpCookie;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceRef;

import com.lolo.json.restlet.JsonMetaData;
import com.lolo.json.restlet.JsonServletRequestHandler;
import com.thoughtworks.xstream.XStream;

public class JsonPoMetaDataDetailsHandler implements JsonServletRequestHandler {

	private String version;
	private String serviceName;
	
	@Override
	public Object executeRequest(String action, HttpServletRequest data) throws IOException {
		if (action.equals("execute")) {
			String model = data.getParameter("model");
			PoMetaDataRequest request = new PoMetaDataRequest();
			request.setValidateRequest(false);
			request.setPoModel(model);
			try {
				URL wsdlLocation = new URL("http://localhost:8889/itcommon/MetaDataPsWs?WSDL");
				QName serviceQName = new QName("http://soa.wgrintra.net/ch/architecture/wsdl", "MetaDataDetailsService");
				Service service = Service.create(wsdlLocation, serviceQName);
				MetaDataDetails port = service.getPort(MetaDataDetails.class);
				return port.execute(request);
			} catch (PresentationServiceException e) {
				throw new IOException("exception in presentation services", e);
			}
		} else {
			throw new IOException("action "+action+" is not defined");
		}
	}

	@Override
	public void configureJsonMapping(String action, XStream xStream) throws IOException {
		xStream.omitField(MetaDataDetailsService.class, "fieldToOmitInTheResponse");  
		xStream.alias("metaDataDetailRequest", PoMetaDataRequest.class);  
		xStream.alias("metaDataDetailResponse", PoMetaDataResponse.class);  
		xStream.alias("codeTables", CodeTable.class);
		xStream.alias("code", Code.class);
		xStream.alias("text", DataText.class);
		xStream.alias("presentationObject", PoData.class);
		xStream.alias("property", PropertyData.class);
		xStream.alias("propertyType", PropertyType.class);
		xStream.alias("valueRange", ValueRange.class);
		
	}

	@Override
	public void getMetaData(JsonMetaData meta) throws IOException {
		meta.addAction("execute", "model").setDescription("get all metadata details of model");
	}

	@Override
	public void setVersion(String version) {
		this.version = version;
	}

	@Override
	public String getVersion() {
		return this.version;
	}

	@Override
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	@Override
	public String getServiceName() {
		return this.serviceName;
	}

}
